#include "mylistwidget.h"
#include "qapplication.h"

#include <Windows.h>
#include<QThread>
#include <QScreen>

#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
MyListWidget::MyListWidget(QListWidget *parent) : QListWidget(parent)
{

  for(int i = 0;i < TargetNumber;i++)
    {
      oneWidget[i] = nullptr;
      item[i] = nullptr;
    }
  create();

  //获取电脑宽度
//  screenWidth = GetSystemMetrics(SM_CXSCREEN);

  //获取屏幕属性
  QScreen *screen = QApplication::primaryScreen();

   screenWidth = screen->size().width();
//  qreal lcd_height = screen->size().height();

  //比例系数
  labelXY = screenWidth/5.5;

  currentWidth = labelXY;

  scaleBody= (double)labelXY/(double)801;

  initListWidget();

  //  双击放大
  connect(this,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(listWidgetDoubleClicked(QListWidgetItem*)));

  //单击选中
  connect(this,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(listWidgeSelectionChanged(QListWidgetItem*)));

}

MyListWidget::~MyListWidget()
{
  for(int i = 1;i < TargetNumber;i++)
    {
      if(oneWidget[i] != nullptr)
        {
          delete oneWidget[i];
        }
      oneWidget[i] = nullptr;

      if(item[i] != nullptr)
        {
          delete item[i];
        }
      item[i] = nullptr;
    }

}

void MyListWidget::create()
{
  setWrapping(true);
  setViewMode(QListView::IconMode);
  setFlow(QListWidget::LeftToRight);
  setResizeMode(QListView::Adjust);
  //最大化
  setWindowState(Qt::WindowMaximized);
}

void MyListWidget::initListWidget()
{
  try{
    for(int i = 1; i < TargetNumber;i++){

        //创建一个靶标对象
        oneWidget[i] = new OneWidget(labelXY);

        item[i] = new QListWidgetItem();

        //设置item大小
        item[i]->setSizeHint(QSize((int)(labelXY*1.051),(int)(labelXY*1.281)));

        //向QListWidget里添加item
        addItem(item[i]);

        //将刚添加的item设置为widget
        setItemWidget(item[i],oneWidget[i]);

        oneWidget[i]->labelSum->setText(QString::asprintf("%1号靶 总环数:%2 中靶数:%3" ).arg(i).arg(0).arg(0));

      }
  }
  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
//    qWarning()<<"MyListWidget-initListWidget"<<ex.what();
    return ;
  }
}

int MyListWidget::getIndex()
{
  return index2;
}

//滚轮响应时间
void MyListWidget::wheelEvent(QWheelEvent *event)
{
  if(event->modifiers() & Qt::ControlModifier)
    {
    QPoint numDegrees;                                     // 定义指针类型参数numDegrees用于获取滚轮转角
    numDegrees = event->angleDelta();                      // 获取滚轮转角
    double step = 0;                                          // 设置中间参数step用于将获取的数值转换成整数型
    if (!numDegrees.isNull())                              // 判断滚轮是否转动
      {
        step = numDegrees.y();                             // 将滚轮转动数值传给中间参数step
      }
    event->accept();                                       // 获取事件

    currentWidth += step/20;                                  // 对当前图像的高累加

    //固定最大最小
    if(currentWidth < 240)
      {
        currentWidth = currentWidth - step/20;
      }else if(currentWidth > 810)
      {
        currentWidth = currentWidth - step/20;
      }

    //获取放缩比例
    scaleBody= (double)currentWidth/(double)801;

    //对每个靶标重新进行大小设置
    for(int i = 1;i < TargetNumber;i++)
      {

        oneWidget[i]->setSize(currentWidth);

        item[i]->setSizeHint(QSize((int)(currentWidth*1.051),(int)(currentWidth*1.291)));

        setItemWidget(item[i],oneWidget[i]);

      }
    //重新排列
    create();
}else{
  int delta = event->angleDelta().y(); // 获取鼠标滚轮的滚动距离
  verticalScrollBar()->setValue(verticalScrollBar()->value() - delta); // 根据滚动距离调整垂直滚动条的位置
  event->accept(); // 接受滚动事件
}

}

//详细信息界面
void MyListWidget::listWidgetDoubleClicked(QListWidgetItem *item)
{

  index2 = currentRow() + 1;

  if(index2 > beginstart && index2 < TargetNumber )
    {
      emit mySignalDoubleIndex(index2);
    }

}

//添加背景阴影
void MyListWidget::listWidgeSelectionChanged(QListWidgetItem* ite)
{
  //标记
  index3 = index2;

  index2 = currentRow() + 1;
  if(index2 > beginstart && index2 < TargetNumber )
    {

      //对当前选中靶标进行深度显示
      item[index2]->setBackground(QColor("#a8a8a8"));
    }

  if(index3 > beginstart && index3 < TargetNumber  && index2 != index3)
    {

      //颜色复位
      item[index3]->setBackground(QColor(Qt::white));
    }

  emit mySignalIndex(index2);
}

//传递子弹数据
void MyListWidget::passHolesData(const QMap<int,QVector<OneTaregtBullet>> &data)
{
  try{

        if(data.empty()){
            for(int i = 1;i < TargetNumber;i++){
                //全部清除
                oneWidget[i]->clearHoles();
                //触发信号
                oneWidget[i]->getMySignal();
                //对靶标信息进行设置
                oneWidget[i]->labelSum->setText(QString::asprintf("%1号靶 总环数:%2 中靶数:%3").arg(i).arg(0).arg(0));

              }
            return ;
          }

    else{
            for(auto it = data.begin();it != data.end();it++){
                //总环数 中靶数
                int sum = 0,zhongBa = 0;

                int addr = it.key();

//                int s_index = addr - 1;

                QVector<OneTaregtBullet>datas = data[addr];

                //确定数组大小
                int msgSize = datas.size();

                if(addr > beginstart && addr < TargetNumber )
                  {

                    oneWidget[addr]->clearHoles();

                    for(int i = 0;i < msgSize;i++)
                      {
                        //对数据进行存储
                        double x = datas[i].x;
                        double y = datas[i].y;
                        double px = x * scaleBody;
                        double py = (801 - y)*scaleBody;

                        //中靶数 中环数 -1指脱靶
                        if(datas[i].direction != -1)
                          {
                            zhongBa++;
                            sum += datas[i].cylinder_number;
                            oneWidget[addr]->addHoles(QPointF(px,py));
                          }

                      }

                    //对靶标信息进行设置
                    oneWidget[addr]->labelSum->setText(QString::asprintf("%1号靶 总环数:%2 中靶数:%3" ).arg(addr).arg(sum).arg(zhongBa));

                    //触发信号
                    oneWidget[addr]->getMySignal();
                  }
                }
        }
  }
  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
    qWarning()<<"MyListWidget:passHolesData"<<ex.what();
    return ;
  }
}

//设置每个靶的状态  未连接  校准中  已连接  就绪
void MyListWidget::passStateData(const QVector<int> &vecState)
{
  try{
    for(int i = 1;i < vecState.size();i++)
      {
            if(vecState[i] == Unconnected)
              {
                oneWidget[i]->labelState->setText(QString::asprintf("未连接"));

                if(currentWidth < 300)
                  {
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 15px;background-color: #cdcdcd}");
                  }else{
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 23px;background-color: #cdcdcd}");
                  }
              }else if(vecState[i] == CalibrationTarget)
              {
                oneWidget[i]->labelState->setText(QString::asprintf("校准中"));

                if(currentWidth < 300)
                  {
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 15px;background-color: #89fff2}");
                  }else{
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 23px;background-color: #89fff2}");
                  }
              }else if(vecState[i] == CommonShooting){
                oneWidget[i]->labelState->setText(QString::asprintf("已连接"));
                //设置字体大小
                if(currentWidth < 300)
                  {
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 15px;background-color: #43ff19}");
                  }else{
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 23px;background-color: #43ff19}");
                  }
              }else if(vecState[i] == ToBeAssigned){
                oneWidget[i]->labelState->setText(QString::asprintf("就绪"));
                //设置字体大小
                if(currentWidth < 300)
                  {
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 15px;background-color: #54ffc6}");
                  }else{
                    oneWidget[i]->labelState->setStyleSheet("QLabel{font-size: 23px;background-color: #54ffc6}");
                  }
              }

            if(currentWidth < 300)
              {
                oneWidget[i]->labelSum->setStyleSheet("QLabel{font-size: 15px;background-color: #cdcdcd}");
              }else{
                oneWidget[i]->labelSum->setStyleSheet("QLabel{font-size: 23px;background-color: #cdcdcd}");
              }

      }

  }
  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
    qWarning()<<"OutWork:passStateData"<<ex.what();
    return ;
  }
}

//传递电量数据
void MyListWidget::passBatteryData(const QVector<double> &msg)
{
  try{
    for(int i = 1;i < msg.size();i++)
      {
        int s_index = i - 1;

        if(s_index >= beginstart && s_index < TargetNumber - 1)
          {

            if(msg[i] < 12.5 && msg[i] != 0){

                oneWidget[s_index]->labelState->setText(QString::asprintf("电量过低"));
                if(currentWidth < 300)
                  {
                    oneWidget[s_index]->labelState->setStyleSheet("QLabel{font-size: 15px;background-color: red}");
                  }else{
                    oneWidget[s_index]->labelState->setStyleSheet("QLabel{font-size: 23px;background-color: red}");
                  }
              }
          }

      }

  }

  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
    qWarning()<<"MyListWidget-passBatteryData"<<ex.what();
    return ;
  }
}







